package com.gress.shop.controller.authority;

import com.gress.shop.annotation.FireLogger;
import com.gress.shop.bo.Action;
import com.gress.shop.bo.MemberRoleRelation;
import com.gress.shop.bo.Role;
import com.gress.shop.bo.RoleActionRelation;
import com.gress.shop.service.IActionService;
import com.gress.shop.service.IRoleService;
import com.gress.shop.util.HappyException;
import com.gress.shop.vo.SwalReturn;
import com.wordnik.swagger.annotations.Api;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created xiongwen with 2018/4/17 19:59
 * Description:
 */
@Controller
@RequestMapping("role")
@Api("角色管理")
public class RoleController {
    @Resource
    private IRoleService roleService;
    @Resource
    private IActionService actionService;

    @RequestMapping("query_all_roles")
    @FireLogger("查询系统角色")
    public String queryAllRoles(Model model) {
        List<Role> roles = roleService.queryAllRoles();
        model.addAttribute(roles);
        List<Action> actions = actionService.queryAllAction();
        model.addAttribute(actions);
        return "role/role";
    }

    @RequestMapping("query_all_member_roles")
    @FireLogger("查询所有用户角色")
    public String queryAllMemberRoles(Model model){
        model.addAttribute(roleService.queryMemberRoles());
        model.addAttribute(roleService.queryAllRoles());
        return "role/role_member";
    }


    @RequestMapping("add_role")
    @FireLogger("新增角色")
    @ResponseBody
    public SwalReturn addRole(Role role,String actionId[]) {
        if (ArrayUtils.isEmpty(actionId)) {
            throw new HappyException("当前角色权限为空");
        }
        roleService.addRole(role, Arrays.asList(actionId));
        return new SwalReturn();
    }

    @RequestMapping("delete_role")
    @FireLogger("删除角色")
    @ResponseBody
    public SwalReturn deleteRole(){
        throw new HappyException("权限不足，请联系管理员");
    }


    @RequestMapping("ajax_modal_role_action")
    @FireLogger("角色权限管理")
    public String ajaxModalRoleAction(String roleId, Model model) {
        List<RoleActionRelation> roleActionRelations = roleService.queryRelationByRoleId(roleId);
        List<Action> actions = actionService.queryAllAction();
        Map<String, Action> map = new HashMap<>();
        actions.forEach(e -> map.put(e.getId(), e));
        roleActionRelations.forEach(e -> map.remove(e.getActionId()));

        model.addAttribute("actionList",map.values());
        model.addAttribute("roleActionRelationList",roleActionRelations);
        model.addAttribute(roleService.queryByRoleId(roleId));
        return "modal/action/role_actions";
    }

    @RequestMapping("delete_role_action")
    @FireLogger("批量删除角色权限")
    @ResponseBody
    public SwalReturn deleteRoleAction(String id[]) {
        if (ArrayUtils.isEmpty(id)) {
            throw new HappyException("最少存在一个权限");
        }
        roleService.deleteRoleAction(Arrays.asList(id));
        return new SwalReturn();
    }

    @RequestMapping("update_role_action")
    @FireLogger("更新角色信息")
    @ResponseBody
    public SwalReturn addRoleAction(String actionId[], Role role) {
        List<String> actionIdList = new ArrayList<>();
        if (!ArrayUtils.isEmpty(actionId)) {
            actionIdList = Arrays.asList(actionId);
        }
        roleService.updateRoleAction(actionIdList, role);
        return new SwalReturn();
    }


    @RequestMapping("add_member_role")
    @FireLogger("更改用户角色信息")
    @ResponseBody
    public SwalReturn addMemberRole(String email, String roleId) {
        roleService.updateMemberRole(email, roleId);
        return new SwalReturn();
    }



}
